<?php
/**
 * Product form definition 
 *
 * @author guzhi
 * @since 2009-03-23
 */

require_once BaseUtil::getModulePath("base")  .  '/forms/BaseForm.php';
require_once   BaseUtil::getModulePath("base")  .  '/validate/Unique.php';

class ProductForm extends BaseForm {
    public function __construct($departments, $product=array()) {
        parent::__construct();
        if(empty($product)) {
            $this->setAction(HttpUtil::getUrl("product", "subadd"));
        } else {
            $this->setAction(HttpUtil::getUrl("product", "subedit"));
        }

        $productId = new Zend_Form_Element_Hidden("id");
        if(isset($product['id'])) {
            $productId->setValue($product['id']);
            $validId = $product['id'];
        } else {
            $validId = null;
        }

        $department = new Zend_Form_Element_Select('department');
        $department->setLabel($this->translate->_('department'));
        $department->setMultiOptions($departments);
        $department->setRequired(true);
        if(isset($product['department_id'])) {
            $department->setValue($product['department_id']);
        }

        $title = new Zend_Form_Element_Text('title');
        $title->setAttrib("id", "title");
        $title->setAttrib("onblur", "javascript:Inventory.copyProductTitle();");
        $title->setLabel($this->translate->_('title'));
        $title->addFilter("StringTrim");
        $title->setRequired(true);
        $validUnique = new MyValid_Unique();
        $validUnique->setFetchAttr('products', 'title', $validId);
        $title->addValidator($validUnique);
        if(isset($product['title'])) {
            $title->setValue($product['title']);
        }

        $urlTitle = new Zend_Form_Element_Text('urlTitle');
        $urlTitle->setAttrib("id", "urlTitle");
        $urlTitle->setAttrib("onblur", "javascript:Inventory.copyProductUrlTitle();");
        $urlTitle->setLabel($this->translate->_('urlTitle'));
        $urlTitle->addFilter("StringTrim");
        $urlTitle->setRequired(true);
        $validUniqueUrl = new MyValid_Unique();
        $validUniqueUrl->setFetchAttr('products', 'url_title', $validId);
        $urlTitle->addValidator($validUniqueUrl);
        if(isset($product['url_title'])) {
            $urlTitle->setValue($product['url_title']);
        }

        $seoTitle = new Zend_Form_Element_Text('seoTitle');
        $seoTitle->setAttrib("id", "seoTitle");
        $seoTitle->setLabel($this->translate->_('seoTitle'));
        $seoTitle->addFilter("StringTrim");
        $seoTitle->setRequired(true);
        if(isset($product['seo_title'])) {
            $seoTitle->setValue($product['seo_title']);
        }

        $seoKeyword = new Zend_Form_Element_Text('seoKeyword');
        $seoKeyword->setLabel($this->translate->_('seoKeyword'));
        $seoKeyword->addFilter("StringTrim");
        //$seoKeyword->setRequired(true);
        if(isset($product['seo_keyword'])) {
            $seoKeyword->setValue($product['seo_keyword']);
        }

        $seoDescription = new Zend_Form_Element_Textarea('seoDescription');
        $seoDescription->setLabel($this->translate->_('seoDescription'));
        $seoDescription->setAttribs(array("rows" => 5, "cols" => 40));
        $seoDescription->addFilter("StringTrim");
        $seoDescription->setRequired(false);
        if(isset($product['seo_description'])) {
            $seoDescription->setValue($product['seo_description']);
        }
        $related1 = new Zend_Form_Element_Text('related1');
        $related1->setLabel($this->translate->_('related1'));
        $related1->addFilter("StringTrim");
        $related2 = new Zend_Form_Element_Text('related2');
        $related2->setLabel($this->translate->_('related2'));
        $related2->addFilter("StringTrim");
        $related3 = new Zend_Form_Element_Text('related3');
        $related3->setLabel($this->translate->_('related3'));
        $related3->addFilter("StringTrim");
        $related4 = new Zend_Form_Element_Text('related4');
        $related4->setLabel($this->translate->_('related4'));
        $related4->addFilter("StringTrim");

        $templete = new Zend_Form_Element_Text('templete');
        $templete->setLabel($this->translate->_('templete'));
        $templete->addFilter("StringTrim");
        if(isset($product['templete'])) {
            $templete->setValue($product['templete']);
        }

        $description = new Zend_Form_Element_Textarea('description');
        $description->setLabel($this->translate->_('description'));
        $description->setAttribs(array("rows" => 8, "cols" => 40));
        $description->addFilter("StringTrim");
        $description->setRequired(false);
        if(isset($product['description'])) {
            $description->setValue($product['description']);
        }

        $chequeRule = new Zend_Form_Element_Select('chequeRule');
        $chequeRule->setLabel($this->translate->_('chequeRule'));
        $chequeRule->setMultiOptions(MetadataConstant::getDefaultChequeRuleList());
        $chequeRule->setRequired("true");
        if(isset($product['cheque_rule'])) {
            $chequeRule->setValue($product['cheque_rule']);
        }

        $priceRule = new Zend_Form_Element_Select('priceRule');
        $priceRule->setLabel($this->translate->_('priceRule'));
        $priceRule->setMultiOptions(MetadataConstant::getDefaultPriceRuleList());
        $priceRule->setRequired("true");
        if(isset($product['price_rule'])) {
            $priceRule->setValue($product['price_rule']);
        }

        $shipRule = new Zend_Form_Element_Select('shipRule');
        $shipRule->setLabel($this->translate->_('shipRule'));
        $shipRule->setMultiOptions(MetadataConstant::getDefaultShipRuleList());
        $shipRule->setRequired("true");
        if(isset($product['ship_rule'])) {
            $shipRule->setValue($product['ship_rule']);
        }

        $relatedRule = new Zend_Form_Element_Select('relatedRule');
        $relatedRule->setLabel($this->translate->_('relatedRule'));
        $relatedRule->setMultiOptions(MetadataConstant::getDefaultRelatedRuleList());
        $relatedRule->setRequired("true");
        if(isset($product['related_rule'])) {
            $relatedRule->setValue($product['related_rule']);
        }
        
        $this->addElements(array($department, $title, $urlTitle, $seoTitle, $seoKeyword, $seoDescription, $templete, $related1,
                $related2, $related3, $related4, $description, $productId,$chequeRule,$priceRule,$shipRule,$relatedRule));
        $this->setElementDecorators(array(
            'ViewHelper',
            array(array('div' => 'HtmlTag'), array('tag' => 'div',"class" => "form_element")),
        ));
    }
}
?>